package com.fenqiyong.www;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fenqiyong.www.fragment.Stateb;
import com.fenqiyong.www.fragment.Stated;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.orhanobut.logger.Logger;
import com.fenqiyong.www.event.CurrentItemEvent;
import com.fenqiyong.www.fragment.Statea;
import com.fenqiyong.www.fragment.Statec;


import es.dmoral.toasty.Toasty;
import hugo.weaving.DebugLog;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import ren.nearby.http.base.BaseActivity;
import ren.nearby.http.event.RxBus2;
import ren.nearby.http.utils.setbar.StatusBarUtils;
import ren.nearby.http.utils.setbar.StatusBarUtils2;


public class MainAct extends BaseActivity implements ViewPager.OnPageChangeListener {


    public SmartTabLayout home_tabLayout;
    public MainViewPager home_viewPager;

    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }


    @Override
    public void initView() {
        super.initView();
        home_tabLayout = findViewById(R.id.home_tabLayout);
        home_viewPager = findViewById(R.id.home_viewPager);
        FragmentPagerItems pages = FragmentPagerItems.with(this)
                .add(R.string.statea, Statea.class)
                .add(R.string.stateb, Stateb.class)
                .create();
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(),
                pages);
        home_viewPager.setOffscreenPageLimit(pages.size());
        home_viewPager.setAdapter(adapter);
        home_viewPager.setScanScroll(true);
        home_viewPager.addOnPageChangeListener(this);
        setup(home_tabLayout);
        home_tabLayout.setViewPager(home_viewPager);
        StatusBarUtils.setStatusBar(this, false, false, 0);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }


    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                StatusBarUtils2.setStatusTextColor(false, MainAct.this);
                break;
            case 1:
                StatusBarUtils2.setStatusTextColor(false, MainAct.this);
                break;

        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @DebugLog
    //添加tab图标
    public void setup(SmartTabLayout layout) {
        layout.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                View view = LayoutInflater.from(MainAct.this).inflate(R.layout.state_menu, container, false);
                ImageView icon = (ImageView) view.findViewById(R.id.state_icon);
                TextView name = (TextView) view.findViewById(R.id.state_name);
                name.setText(adapter.getPageTitle(position));
                switch (position) {
                    case 0:
                        icon.setImageDrawable(getResources().getDrawable(R.drawable.state_selected_1));
                        break;
                    case 1:
                        icon.setImageDrawable(getResources().getDrawable(R.drawable.state_selected_2));
                        break;
                    default:
                        throw new IllegalStateException("Invalid position: " + position);
                }
                return view;
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }

    }

    private boolean isExit = false;

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toasty.info(getActivity(), "再按一次退出程序").show();
        } else {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            finish();
            System.exit(0);
        }
    }
}

